package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.DamageListService;
import com.atguigu.jxc.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-29 16:13 星期日
 * description:
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    DamageListService damageListService;

    @Autowired
    DamageListGoodsDao damageListGoodsDao;

    @Autowired
    UserDao userDao;

    @Autowired
    GoodsService goodsService;

    /**
     * 保存报损单
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(DamageList damageList, String damageListGoodsStr ) {

        //将json数组转换为字符串
        List<DamageListGoods> damageListGoodsList = JSONArray.parseArray(damageListGoodsStr,DamageListGoods.class);

        //获取当前用户id
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        damageList.setUserId(currentUser.getUserId());

        damageListService.save(damageList);
        Integer damageListId = damageList.getDamageListId();

        for (DamageListGoods damageListGoods : damageListGoodsList) {
            damageListGoods.setDamageListId(damageListId);
            //商品损坏后需要修改库存（库存总量 - 损毁数量）
            Integer goodsNum = damageListGoods.getGoodsNum();
            String goodsCode = damageListGoods.getGoodsCode();

            //减库存 （需要传入负数）
            boolean ifChange = goodsService.changeInventoryQuantity(goodsCode, -goodsNum);

            if (ifChange == false){
                return new ServiceVO(ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR,ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR_MSG);
            }
        }

        //批量保存damageListGoods
        damageListGoodsDao.bachSave(damageListGoodsList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }

    /**
     * 报损单查询
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String sTime, String eTime) {

        Map<String,Object> map = new HashMap<>();

        List<DamageList> damageList = damageListService.list(sTime,eTime);
        map.put("rows",damageList);
        return map;
    }

    /**
     * 查询报损单商品信息
     * @param damageListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        Map<String,Object> map = new HashMap<>();
        List<DamageListGoods> listGoods = damageListGoodsDao.goodsList(damageListId);
        map.put("rows",listGoods);
        return map;
    }
}
